<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
    <style>
        .app {
            width: 500px;
            margin: 100px auto;
        }
    </style>
</head>

<body>
    <div class="app">
        <div>
            <input type="text" v-model="money">
            <input type="button" value="货币转换" @click="active_ren">
        </div>
        <h1>{{money_change | change}} $</h1>
        <h1>{{money_change | change_mei}} ￥</h1>
    </div>
    <script>
        const vm = new Vue({
            data: {
                money: 0,
                money_change: 0,
            },
            methods: {
                active_ren() {
                    this.money_change = this.money
                }
            },
            filters: {
                change(value) {
                    if (!value) return 0;
                    value = value / 6.33
                    value = value.toFixed(4)
                    value = value.toString().split(".")
                    let value_left = value[0].split("").reverse()
                    let a = 3;
                    for (let i = 0; i < value_left.length; i++) {
                        if (i == a) {
                            value_left.splice(i, 0, ",")
                            a = a + 4
                        }
                    }


                    // value_left.splice(8, 0, ",")
                    value_left.reverse()
                    let value_right = value[1]
                    value = value_left.join('').concat("." + value_right)
                    return value
                },
                change_mei(value) {
                    if (!value) return 0;
                    value = value * 6.33
                    value = value.toFixed(4)
                    value = value.toString().split(".")
                    let value_left = value[0].split("").reverse()
                    let a = 3;
                    for (let i = 0; i < value_left.length; i++) {
                        if (i == a) {
                            value_left.splice(i, 0, ",")
                            a = a + 4
                        }
                    }

                    // value_left.splice(8, 0, ",")
                    value_left.reverse()
                    let value_right = value[1]
                    value = value_left.join('').concat("." + value_right)
                    return value
                }
            },
        }).$mount(".app")
    </script>
</body>

</html>